﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Reflection;

/// <summary>
/// Summary description for DatabaseUtil
/// </summary>
public static class DatabaseUtil
{
    static DatabaseUtil()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public static DataTable ToDataTable<T>(this List<T> list)
    {
        DataTable dt = new DataTable();
        if (list.Count > 0)
        {
            Type listType = list.ElementAt(0).GetType();
            //Get element properties and add datatable columns  
            PropertyInfo[] properties = listType.GetProperties();
            foreach (PropertyInfo property in properties)
                dt.Columns.Add(new DataColumn() { ColumnName = property.Name });
            foreach (object item in list)
            {
                DataRow dr = dt.NewRow();
                foreach (DataColumn col in dt.Columns)
                    dr[col] = listType.GetProperty(col.ColumnName).GetValue(item, null);
                dt.Rows.Add(dr);
            }
        }
        return dt;
    }

    public static String GetNextSequence(String str, int length)
    {
        long l = long.Parse(str);
        l++;
        String tmp = l.ToString();
        if (tmp.Length < str.Length)
        {
            return StringUtil.PadLeft(tmp, length, '0');
        }
        else
        {
            return tmp;
        }
    }
    public static string GetNextCode<T>(int length = 5)
    {
        string tableName = typeof(T).Name;
        DBDataContext db = new DBDataContext();
        String sql = "SELECT TOP 1 * FROM " + tableName + " ORDER BY ID DESC";
        var t = db.ExecuteQuery<T>(sql);
        T o = t.FirstOrDefault<T>();
        if (o != null)
        {
            string code = o.GetType().GetProperty(tableName + "Code").GetValue(o, null).ToString();
            return GetNextSequence(code, length);
        }
        else
        {
            string str = String.Empty;
            for (int i = 0; i < length - 1; i++)
            {
                str += "0";
            }
            str += "1";
            return str;
        }
    }



}

public enum DataTableName
{
    F01_Shift,
    F02_AdjustedIncentivePoints,
    F02_AppliedIncentivePoints,
    F02_ConversionNumbers,
    F02_DailyEmployeeIncentivePoints,
    F02_DailyIncentivePoints,
    F02_IncentiveHierachies,
    F02_IncentivePointTargets,
    F02_IncentiveProportions,
    F02_IncentiveTargetPercents,
    F02_IncentiveTargets,
    F02_JobTitleDensities,
    F02_JobTitleDensityHierachies,
    F02_MonthlyEmployeeIncentivePoints,
    F02_SalesDetail,
    F02_SalesmanIncentivePoints,
    F02_ShopGroupDensities,
    F02_ShopGroups,
    F02_StandardIncentivePoints,
    F02_SupporterIncentivePoints,
    F03_EmployeeContracts,
    F03_Employees,

    F03_JobTitleEmployees,
    F03_JobTitleJobRequirements,
    F03_JobTitleOrganizationHierachies,
    F03_RegionsByOrganizationHierachyCode,
    F03_RegionByLevel,
    F03_JobTitleByOrganizationHierachyCode,
    F03_GetEmployeeGroupByWarehouseCode,
    F03_JobTitleRegions,
    F03_JobTitles,
    F03_JobTitleSalaries,
    F03_OrganizationHierachies,
    F03_OrganizationHierachiesB1,
    F03_RegionHierachies,
    F03_RegionHierachiesLevel1,
    F03_ShopByRegionCode,
    F03_GetJobTitleByEmployeeCode,
    F03_GetShopIsMasterByEmployeeCode,
    //
    WarehouseByRegionCode,
    ProductHierarchyByLevel,
    ProductHierarchyLevel456,
    WarehouseByEmployeeCode,
    Warehouse,
    ManageUserB1,
    F05_GetRegionHierachiesContainASM,
    F03_GetRegionHierachiesForCombo,
    F04_GetRoleUser,
    Function,
    F05_UserRequest
}

public static class ProductHierarchyType
{
    public const String Branch = "N";
    public const String Manufacture = "H";
    public const String Type = "L";
}

public static class ObjectStatus
{
    public const Char Active = 'A';
    public const Char Invisible = 'I';
    public const Char Online = 'O';
    public const Char Delete = 'D';
}

public static class PaymentType
{
    public const String Cash = "C";
    public const String Bank = "B";
}

public static class DeliveryPlanType
{
    public const String Contract = "C";
    public const String PurchaseOrder = "P";
}

public static class CMCodeCodeDetail
{
    public const String OrderRequestPurpose = "OrderRequestPurpose";
   
}

public static class JobTitleShortName
{
    public const String ASM = "ASM";
    public const String RSM = "RSM";
    public const String SM = "SM";
    public const String PSM = "PSM";
}

public static class Roles
{
    public const String Admin = "Admin"; // Quan tri he thong

}

public static class PrimaryKeyLenght
{
    public const int ProductCode = 7;
    public const int ShopGroupCode = 5;
}

public static class AdjustType
{
    public const String Future = "Future";
    public const String Past = "Past";
    public const String Current = "Current";
}

public static class FunctionType
{
    public const String Function = "F";
    public const String Report = "R";
}

public static class SystemFunction
{
    public const String Inside = "INS";
    public const String SRM = "CRM";
}

public static class IncentiveProductOldTargetsType
{
    public const String Sell = "S";
    public const String Purchase = "P";
}

public static class IncentiveType
{
    public const String ProductHierarchyTarget = "P";
    public const String FinanceTarget = "F";
    public const String FinanceProductHierarchyTarget = "FP";
}